<?php

namespace Home\Controller; 
use Think\Controller;

 
class NewsController extends  BaseController { 
     function _initialize(){
        parent::_initialize(); 
    }
    
    //新闻列表页面
    public  function  index(){
       // ki_article_cat   精品文章   cat_id = 17 
       $catid = empty($_GET['cat_id'])? 20 : $_GET['cat_id'];
       $this->assign('catid',$catid);
       $this->assign('pagetitle','精品文章');   
       $this->display();
    } 

     public function newslist(){
         // ki_article_cat   精品文章   cat_id = 17 
       $msg=M("article");
         if (!empty($_GET['pageNow'])) {
            $pageNow = $_GET['pageNow'];
        } else {
            $pageNow = 1;
        }
         $pageSize = 20;
		 $where['cat_id'] = empty($_GET['cat_id'])? "" : $_GET['cat_id'];
         $pageNow = $_GET['pageNow'];
          $count =$msg->where($where)->count(); // 查询满足要求的总记录数
          $numPage =ceil($count/$pageSize) ;
          
          if($pageNow > $numPage){
             echo "";  
          }
       
        $pages = $pageSize * ($pageNow - 1)<=0 ? 0 :$pageSize * ($pageNow - 1) ;
        $ret=$msg->where($where)->field('article_id,title,cat_id')->limit( $pages,$pageSize )->order("sort desc")->select(); 
        $list=array();
        foreach ($ret as $k=>$v){ 
        	$v['url']= U("News/details",array('id'=>$v['article_id']));
        	$list[]=$v;
        }
        
       if($list&&count($list)>0){
         echo json_encode($list);
       }else{
          echo ""; 
       }
    }
   
    public function details(){
        $where['article_id'] = empty($_GET['id'])? "" : $_GET['id'];
          $list =  D('article')->where($where)->find();
          $list['date'] = date('m-d H:i:s',$list['add_time']);
          if($list != false){
            $this->assign('list',$list);
            $this->display();
            }else{
              $this->merror('无效连接');  
            }      
    
    }


//其余文章
    public function  info(){
    	if(!IS_GET) $this->merror('请求访问错误!');
    	 
    	$where['cat_id'] = empty($_GET['cat_id'])? "" : $_GET['cat_id'];
    	$ret = D('article_cat')->field('cat_name')->where($where)->find();
    	if($ret == flase){
    		$this->merror('参数错误!');
    	}
    	 
    	$where['is_open'] = 1 ;
    	$list = D('article')->where($where)->order('article_id DESC')->find();
    	if($list != false){
    		$list['date'] = date('Y-m-d H:i:s',$list['add_time']);
    	}
    	 
    	$this->assign('pagetitle',$ret['cat_name']);
    	$this->assign('list',$list);
    	$this->display();
    	
    	/*
      if(IS_GET){
        $where['cat_id'] = empty($_GET['cat_id'])? "" : $_GET['cat_id'];
        $title = D('article_cat')->field('cat_name')->where($where)->find();
        if($title == flase){
          $this->merror('无效连接');
        }
        $where['is_open'] = 1 ;
        $list = D('article')->where($where)->order('article_id DESC')->find();
            if($list != false){
             $list['date'] = date('Y-m-d H:i:s',$list['add_time']);
              } 
               $this->assign('pagetitle',$title['cat_name']);   
               $this->assign('list',$list);
               $this->display();
     }else{
      $this->merror('无效连接');
     }
     */ 
     }
}
?>
